#include "parser.h"
#include "PKB.h"


Parser::Parser() : statementCount( 0 )
{
	codeList="";
	errorMsg="";
}
Parser::~Parser()
{

}
bool Parser::parse(string str)
{
	statementCount=0;
	tokenizer = StringTokenizer(str);

	return runParser();
}
bool Parser::runParser()
{
	//calls it, to ensure it is empty, incase we used Parser multiple times on different sources;
	callValidator.empty();
	while (true)
	{
		string s=tokenizer.nextToken();
		//end of file stream
		if (matcher.matchExact(s,""))
			break;
		//check for procedure token marker, "procedure"
		//and transfer parsing to createProcedure
		else if (matcher.matchExact(s,"procedure"))
		{
			
			TNode *temp = createProcedure();
			if (temp==NULL)
				return false;
		}
		else
		{
			//invalid syntax, improperly defined procedure
			//need to be handled
			errorMsg = "error invalid token detected, but \"procedure\" is expected definition after line: "+statementCount;
			return false;
		}
	}
	//validating call statements, eg, first procedure has a call to second procedure, only can validate after complete parsing
	while (!callValidator.empty())
	{
		if (PKB::procTable.GetProcIndex(callValidator.front())==-1)
		{
			//cout << "invalid call " << callValidator.front() << endl;
			errorMsg = "error, invalid call operation found";
			return false;
		}
		else
		{
			callValidator.pop();
		}
	}
	return true;
}

TNode* Parser::createProcedure()
{
	int startStmtCount=0;
	int endStmtCount=0;
	TNode* procNode = NULL;
	string procName = tokenizer.nextToken();
	if (matcher.matchProcedureName(procName))
	{
		startStmtCount=statementCount;
		PROCINDEX procIndex = PKB::procTable.InsertProc(procName);
		procNode = PKB::ast.CreateNode(NODETYPE::PROCEDURE);
		PKB::ast.SetValue(procNode,procName);
		//perform header
		//expect {
		string bracket=tokenizer.nextToken();
		if (matcher.matchExact(bracket,"{"))
		{
			//valid syntax, handle
		}
		else
		{

			//invalid syntax, no open brackets
			errorMsg = "missing { at line: "+statementCount;
			return NULL;
		}
		//valid syntax from above, transfer parsing to createStatementList
		PKB::ast.CreateAST(procNode,procIndex);
		TNode* temp = createStatementList(procNode,0);
		if (temp==NULL)
			return NULL;

		endStmtCount=statementCount;
		//cout << "procedure detected at, " << startStmtCount+1 << ":" << endStmtCount << endl;
		PKB::procTable.SetProcStatementRange(procIndex,startStmtCount+1,endStmtCount);
	//	roots.push_back(procNode);
	}
	else
	{
		errorMsg = "invalid procedure name at line: "+statementCount;
		return NULL;
		//invalid procedure name
	}
	return procNode;
}
TNode* Parser::createStatementList(TNode* parentNode, int nchild)
{
	string s="";
	int startCount=statementCount;
	int counter=0;
	TNode* prevStmt = NULL;
	TNode* currStmt = NULL;
	//start creating statement
	TNode* stmtList = PKB::ast.CreateNode(NODETYPE::STMTLST);
	PKB::ast.SetValue(stmtList,"stmtList");
	PKB::ast.SetParentChildLink(parentNode,stmtList,nchild);
	while (true)
	{
		s=tokenizer.nextToken();
		//end of statement list
		if (matcher.matchExact(s,""))
		{
			//abrupt end, syntax error
			errorMsg = "unexpected end of file line: "+statementCount;
			return NULL;
		}
		else if (matcher.matchExact(s,"}"))
		{
			//end of statement list
			PKB::ast.SetRightSibling(currStmt,NULL);
			break;
		}
		else
		{
			currStmt = createStatement(s,stmtList,counter);
			counter++;
			if (currStmt==NULL)
				return NULL;
			if (prevStmt!=NULL)
				PKB::ast.SetRightSibling(prevStmt,currStmt);
			prevStmt=currStmt;
		}

	}
	int endCount = statementCount;
	if (endCount-startCount<1)
	{		
		//empty statementList error
		//need handle this
		errorMsg = "error, cannot have empty statement list, line: "+startCount;
		return NULL;
	}
	return stmtList;
}
TNode* Parser::createStatement(string previousToken,TNode* parentNode,int nchild)
{
	//increment statementCount
	statementCount++;
	TNode* temp = NULL;
	if (matcher.matchExact(previousToken,"while"))
	{
		//handle while loop
		temp = createWhile(parentNode,nchild);
	}
	else if (matcher.matchExact(previousToken,"if"))
	{
		//handle if container
		temp = createIf(parentNode,nchild);
	}
	else if (matcher.matchExact(previousToken,"call"))
	{
		//handle call statement
		temp = createCall(parentNode,nchild);
	}
	else if (matcher.matchVarName(previousToken))
	{
		//handle assignment statement
		temp = createAssign(previousToken,parentNode,nchild);
	}
	else
	{
		errorMsg = "error invalid syntax at line: "+statementCount;
		return NULL;
		//invalid syntax, could be anything, from empty space to premature { or } or even ;
	}
	return temp;
}


TNode* Parser::createWhile(TNode* parentNode, int nchild)
{
	
	//create while node and set to parent, the stmtlst
	TNode* whileNode = PKB::ast.CreateNode(NODETYPE::WHILE);
	PKB::ast.SetStatementNumber(whileNode,statementCount);
	PKB::ast.SetParentChildLink(parentNode,whileNode,nchild);
	//expect var
	string var = tokenizer.nextToken();
	if (matcher.matchVarName(var))
	{
		//insert var into vartable and create varNode, insert as first child of whileNode
		VARINDEX varIndex = PKB::varTable.InsertVar(var);
		TNode* varNode = PKB::ast.CreateNode(NODETYPE::VARIABLE);
		PKB::ast.SetValue(varNode,var);
		PKB::ast.SetParentChildLink(whileNode,varNode,0);
		//expect { for stmtlst
		string s = tokenizer.nextToken();
		if (matcher.matchExact(s,"{"))
		{
			//valid syntax
			//create statementList, and also stmtLst will be second child of whileNode
			TNode* temp = createStatementList(whileNode,1);
			if (temp==NULL)
				return NULL;
		}
		else
		{
			//expect { but didn't get it, error in syntax
			errorMsg = "error { missing from statement at line: "+statementCount;
			return NULL;
		}
	}
	else
	{
		//invalid token, could be invalid name or even abrupt end, error in syntax
		errorMsg = "error invalid token or abrupt end at line: "+statementCount;
		return NULL;
	}
	return whileNode;
}

TNode* Parser::createIf(TNode* parentNode, int nchild)
{
	
	//create while node and set to parent, the stmtlst
	TNode* ifNode = PKB::ast.CreateNode(NODETYPE::IF);
	PKB::ast.SetStatementNumber(ifNode,statementCount);
	PKB::ast.SetParentChildLink(parentNode,ifNode,nchild);
	//expect var
	string var = tokenizer.nextToken();
	if (matcher.matchVarName(var))
	{
		//insert var into vartable and create varNode, insert as first child of whileNode
		VARINDEX varIndex = PKB::varTable.InsertVar(var);
		TNode* varNode = PKB::ast.CreateNode(NODETYPE::VARIABLE);
		PKB::ast.SetValue(varNode,var);
		PKB::ast.SetParentChildLink(ifNode,varNode,0);
		//expect { for stmtlst
		string s = tokenizer.nextToken();
		if (matcher.matchExact(s,"then")&&matcher.matchExact(tokenizer.nextToken(),"{"))
		{
			//valid syntax
			//create statementList, and also stmtLst will be then stmtList of if container
			TNode* temp = createStatementList(ifNode,1);
			if (temp==NULL)
				return NULL;
			//processed stmtList without problems, proceeding to process else stmtList of if container
			string expectedTokenElse = tokenizer.nextToken();
			string expectedTokenOpenBracket = tokenizer.nextToken();
			if (!matcher.matchExact(expectedTokenElse,"else"))
			{
				//encountered keyword else
				errorMsg = "error else missing from statement at line: "+statementCount;
				return NULL;
			}
			else if (!matcher.matchExact(expectedTokenOpenBracket,"{"))
			{
				//expect { but didn't get it, error in syntax
				errorMsg = "error { missing from statement at line: "+statementCount;
				return NULL;
			}
			else
			{
				TNode* temp2 = createStatementList(ifNode,2);
				if (temp2==NULL)
					return NULL;
			}
		}
		else
		{
			//expect { but didn't get it, error in syntax
			errorMsg = "error { missing from statement at line: "+statementCount;
			return NULL;
		}
	}
	else
	{
		//invalid token, could be invalid name or even abrupt end, error in syntax
		errorMsg = "error invalid token or abrupt end at line: "+statementCount;
		return NULL;
	}
	return ifNode;
}

TNode* Parser::createCall(TNode* parentNode, int nchild)
{
	string procName = tokenizer.nextToken();
	TNode* callNode = NULL;
	if (matcher.matchProcedureName(procName))
	{
		callNode = PKB::ast.CreateNode(NODETYPE::CALL);
		PKB::ast.SetStatementNumber(callNode,statementCount);
		PKB::ast.SetValue(callNode,procName);
		PKB::ast.SetParentChildLink(parentNode,callNode,nchild);
		string expectSemiColon = tokenizer.nextToken();
		if (!matcher.matchExact(expectSemiColon,";"))
		{
			errorMsg = "error expect ; at line: "+statementCount;
			return NULL;
		}
	}
	else
	{
		errorMsg = "invalid error msg at line: "+statementCount;
		return NULL;
	}
	callValidator.push(procName);
	return callNode;
}

TNode* Parser::createAssign(string previousToken,TNode* parentNode,int nchild)
{
	//create assignment statement
	TNode* assignNode = NULL;
	TNode* exprNode = NULL;
	//check if its =
	string equals = tokenizer.nextToken();
	if (matcher.matchExact(equals,"="))
	{
		//create assignNode and attach to parentNode
		assignNode = PKB::ast.CreateNode(NODETYPE::ASSIGN);
		PKB::ast.SetStatementNumber(assignNode,statementCount);
		PKB::ast.SetValue(assignNode,equals);
		PKB::ast.SetParentChildLink(parentNode,assignNode,nchild);
		//insert assigned variable to vartable, then create it as child node and attach to assignNode, as first child
		PKB::varTable.InsertVar(previousToken);
		TNode* varNode = PKB::ast.CreateNode(NODETYPE::VARIABLE);
		PKB::ast.SetValue(varNode,previousToken);
		PKB::ast.SetParentChildLink(assignNode,varNode,0);
		//create the rest of the expression
		exprNode = createExpressionV2(assignNode);

		if (exprNode==NULL)
		{
			cout << "error at line: " + statementCount << endl;
			return NULL;
		}
		PKB::ast.SetParentChildLink(assignNode,exprNode,1);
	}
	else
	{
		errorMsg = "error missing \"=\" operator at line: "+statementCount;
		return NULL;
		//error, = symbol not found
	}
	//cout << PKB::ast.GetValue(assignNode) << endl;
	return assignNode;
}

TNode* Parser::createExpression()
{
	TNode* subTreeRoot = NULL;
	TNode* currentNode = NULL;
	string currentToken = "";
	string previousToken = "";
	while (true)
	{
		currentToken = tokenizer.nextToken();
		if (matcher.matchInteger(currentToken))
		{
			currentNode = PKB::ast.CreateNode(NODETYPE::CONSTANT);
			PKB::ast.SetValue(currentNode,currentToken);
			if (subTreeRoot==NULL)
			{
				subTreeRoot=currentNode;
			}
			else
			{
				if (PKB::ast.GetNodeType(subTreeRoot)==NODETYPE::PLUS)
				{
					PKB::ast.SetParentChildLink(subTreeRoot,currentNode,1);
				}
				else
				{
					//error handle, as syntax violated
					
					errorMsg = "error, cannot allow 2 variables/constants to be put side by side, violation of syntax at line: "+statementCount;
					return NULL;
				}
			}
		}
		else if (matcher.matchVarName(currentToken))
		{
			currentNode = PKB::ast.CreateNode(NODETYPE::VARIABLE);
			PKB::ast.SetValue(currentNode,currentToken);
			PKB::varTable.InsertVar(currentToken);
			if (subTreeRoot==NULL)
			{
				subTreeRoot=currentNode;
			}
			else
			{
				if (PKB::ast.GetNodeType(subTreeRoot)==NODETYPE::PLUS)
				{
					PKB::ast.SetParentChildLink(subTreeRoot,currentNode,1);
				}
				else
				{
					//error handle, as syntax violated
					errorMsg = "error, cannot allow 2 variables/constants to be put side by side, violation of syntax at line: "+statementCount;
					return NULL;
				}
			}
		}
		else if (matcher.matchOperator(currentToken))
		{
			currentNode = PKB::ast.CreateNode(NODETYPE::PLUS);
			PKB::ast.SetValue(currentNode,currentToken);
			if (subTreeRoot==NULL)
			{
				//syntax violated, eg x = +.....
				errorMsg = "error, cannot have \"+\" as first token line: "+statementCount;
				return NULL;
			}
			else
			{
				if (PKB::ast.GetNodeType(subTreeRoot)==NODETYPE::PLUS||
					PKB::ast.GetNodeType(subTreeRoot)==NODETYPE::CONSTANT||
					PKB::ast.GetNodeType(subTreeRoot)==NODETYPE::VARIABLE)
				{
					PKB::ast.SetParentChildLink(currentNode,subTreeRoot,0);
					subTreeRoot=currentNode;
				}
				else
				{
					//invalid node detected
					errorMsg = "error, expected token \"+\" not found at line: "+statementCount;
					return NULL;
				}
			}
		}
		else if (matcher.matchExact(currentToken,";"))
		{
			break;
		}
		else
		{
			//error in syntax
			errorMsg = "error, invalid token that is not variable/constant detected at line: "+statementCount;
			return NULL;
		}
		previousToken=currentToken;
	}
	return subTreeRoot;
}
TNode* Parser::createExpressionV2(TNode* parentNode)
{
	int openParenthesisEncountered = 0;
	//int closedParenthesisEncountered = 0;
	string previousToken="";
	string currentToken="";
	string postfixNotationString = "";
	//perform validation of expression
	queue<string> infixNotationQueue;
	
	while (true)
	{
		currentToken = tokenizer.nextToken();
		if (matcher.matchExact(currentToken,""))
		{
			//error sudden end of file encountered
			errorMsg = "sudden end of file encountered";
			return NULL;
		}
		if (matcher.matchExact(currentToken,";"))
		{
			//reached end of statement
			if (matcher.matchExact(previousToken,""))
			{
				//eg x = ;
				errorMsg = "invalid expression at line: "+statementCount;
				return NULL;
			}
			else if (matcher.matchExact(previousToken,"(")||matcher.matchOperator(previousToken))
			{
				//eg x = a+;
				//eg x = a+(;
				errorMsg = "malformed expression at line: "+statementCount;
				return NULL;
			}
			break;
		}
		if (matcher.matchExact(previousToken,""))
		{
			//first item to be handled
			if (matcher.matchConstValue(currentToken)
				||matcher.matchVarName(currentToken))

			{
				infixNotationQueue.push(currentToken);
			}
			else if (matcher.matchExact(currentToken,"("))
			{
				openParenthesisEncountered++;
				infixNotationQueue.push(currentToken);
			}
			else
			{
				//invalid tokens encountered
				errorMsg = "invalid token at line: "+statementCount;
				return NULL;
			}
		}
		else
		{
			//subsequent items to be handled
			if (matcher.matchExact(currentToken,"("))
			{
				//handle token (
				if (matcher.matchExact(previousToken,"(")||matcher.matchOperator(previousToken))
				{
					infixNotationQueue.push(currentToken);
					openParenthesisEncountered++;
				}
				else
				{
					//error msg
					errorMsg = "cannot be preceded by const or varName or ) at line: "+statementCount;
					return NULL;
				}


			}
			else if (matcher.matchExact(currentToken,")"))
			{
				//handle token )
				if (openParenthesisEncountered<=0)
				{
					errorMsg = "no ( preceding ), invalid expression at line: "+statementCount;
					return NULL;
				}
				else if (matcher.matchVarName(previousToken)||matcher.matchExact(previousToken,")")||matcher.matchConstValue(previousToken))
				{
					infixNotationQueue.push(currentToken);
					openParenthesisEncountered--;
				}
				else
				{
					errorMsg = "cannot have ( + - * / before ), at line: "+statementCount;
					return NULL;
				}
			}
			else if (matcher.matchOperator(currentToken))
			{
				//handle token + - * /
				if (matcher.matchConstValue(previousToken)||matcher.matchVarName(previousToken)||matcher.matchExact(previousToken,")"))
				{
					infixNotationQueue.push(currentToken);
				}
				else
				{
					errorMsg = "cannot have ( + - * /, at line: "+statementCount;
					return NULL;
				}
			}
			else if (matcher.matchConstValue(currentToken)||matcher.matchVarName(currentToken))
			{
				//handle constant or variables
				if (matcher.matchOperator(previousToken)||matcher.matchExact(previousToken,"("))
				{
					infixNotationQueue.push(currentToken);
				}
				else
				{
					errorMsg = "cannot have ) varName const, at line: "+statementCount;
					return NULL;
				}
			}
			else
			{
				//invalid token encountered
				errorMsg = "invalid token encountered at line: "+statementCount;
				return NULL;
			}
		}
		previousToken=currentToken;
	}

	//check if the parenthesis is done properly
	if (openParenthesisEncountered!=0)
	{
		errorMsg = "invalid parenthesis in expression at line: "+statementCount;
		return NULL;
	}
	/*
	cout << "start of expressionV2------------" << endl;
	queue<string> infix_temp;
	while (!infixNotationQueue.empty())
	{
		cout << infixNotationQueue.front();
		infix_temp.push(infixNotationQueue.front());
		infixNotationQueue.pop();
	}
	while (!infix_temp.empty())
	{
		infixNotationQueue.push(infix_temp.front());
		infix_temp.pop();
	}
	*/
	//perform shunting yard algorithm
	
	stack<string> shuntingYardStack;
	queue<string> postfixNotationQueue;

	while (!infixNotationQueue.empty())
	{
		string shunting_temp = infixNotationQueue.front();
		infixNotationQueue.pop();
		if (matcher.matchConstValue(shunting_temp)||matcher.matchVarName(shunting_temp))
		{
			//const or varName encountered
			postfixNotationQueue.push(shunting_temp);
		}
		else if (matcher.matchOperator(shunting_temp))
		{
			//operator encountered
			while (!shuntingYardStack.empty())
			{
				if (matcher.getOperatorPrecedence(shunting_temp)<=matcher.getOperatorPrecedence(shuntingYardStack.top()))
				{
					postfixNotationQueue.push(shuntingYardStack.top());
					shuntingYardStack.pop();
				}
				else
				{
					break;
				}
			}
			shuntingYardStack.push(shunting_temp);
			
		}
		else if (matcher.matchExact(shunting_temp,"("))
		{
			// ( encountered
			shuntingYardStack.push(shunting_temp);
		}
		else if (matcher.matchExact(shunting_temp,")"))
		{
			// ) encountered
			//pop all operators onto postfixNotationQueue, until we find the (
			while (!matcher.matchExact(shuntingYardStack.top(),"("))
			{
				postfixNotationQueue.push(shuntingYardStack.top());
				shuntingYardStack.pop();
			}
			//finally pop the (
			shuntingYardStack.pop();
		}
	}
	while (!shuntingYardStack.empty())
	{
		postfixNotationQueue.push(shuntingYardStack.top());
		shuntingYardStack.pop();
	}
	/*
	cout << endl;
	cout << "shunting yard---------------" << endl;
	queue<string> temp_queue;
	string temp_checker;
	while (!postfixNotationQueue.empty())
	{
		temp_checker = postfixNotationQueue.front();
		postfixNotationQueue.pop();
		cout << temp_checker;
		temp_queue.push(temp_checker);
	}
	cout << endl;
	*/
	//convert postfix notation to AST
	stack<TNode*> expressionStack;
	while (!postfixNotationQueue.empty())
	{
		if (postfixNotationString.length()==0)
		{
			postfixNotationString+=postfixNotationQueue.front();
		}
		else
		{
			postfixNotationString +=",";
			postfixNotationString +=postfixNotationQueue.front();
		}
		string postfixToken = postfixNotationQueue.front();
		postfixNotationQueue.pop();
		if (matcher.matchConstValue(postfixToken))
		{
			//const
			TNode* constNode = PKB::ast.CreateNode(NODETYPE::CONSTANT);
			PKB::ast.SetValue(constNode,postfixToken);
			expressionStack.push(constNode);
		}
		else if (matcher.matchVarName(postfixToken))
		{
			//var
			TNode* varNode = PKB::ast.CreateNode(NODETYPE::VARIABLE);
			PKB::ast.SetValue(varNode,postfixToken);
			PKB::varTable.InsertVar(postfixToken);
			expressionStack.push(varNode);
		}
		else
		{
			//operator
			TNode* opNode = NULL;
			TNode* leftOperand = NULL;
			TNode* rightOperand = NULL;
			if (matcher.matchExact(postfixToken,"+"))
				opNode = PKB::ast.CreateNode(NODETYPE::PLUS);
			else if (matcher.matchExact(postfixToken,"-"))
				opNode = PKB::ast.CreateNode(NODETYPE::MINUS);
			else if (matcher.matchExact(postfixToken,"*"))
				opNode = PKB::ast.CreateNode(NODETYPE::TIMES);
			else if (matcher.matchExact(postfixToken,"/"))
				opNode = PKB::ast.CreateNode(NODETYPE::DIVIDE);
			else
			{
				errorMsg = "unrecognised operator, although the program should not even get here";
				return NULL;
			}
			PKB::ast.SetValue(opNode,postfixToken);
			if (expressionStack.empty())
			{
				errorMsg = "error stack is lacking of operands";
				return NULL;
			}
			else
			{
				rightOperand = expressionStack.top();
				expressionStack.pop();
			}
			if (expressionStack.empty())
			{
				errorMsg = "error stack is lacking of operands";
				return NULL;
			}
			else
			{
				leftOperand = expressionStack.top();
				expressionStack.pop();
			}
			PKB::ast.SetParentChildLink(opNode,leftOperand,0);
			PKB::ast.SetParentChildLink(opNode,rightOperand,1);
			expressionStack.push(opNode);
		}
	}
	if (expressionStack.empty())
	{
		return NULL;
	}
	else
	{
		string finalPostfixNotationString = ","+postfixNotationString+",";
		TNode* root = expressionStack.top();
		expressionStack.pop();
		PKB::ast.SetValue(parentNode,finalPostfixNotationString);
		return root;
	}
}

void Parser::recreateCode()
{
	/*
	for (int i=0;i<roots.size();i++)
	{
		//displayProcedure(roots[i],procedureAST[i]);
	}
	*/
}
void Parser::displayProcedure(TNode* node)
{
	PROCINDEX pindex = PKB::procTable.GetProcIndex(PKB::ast.GetValue(node));

	//cout << "first:last " << pkb->GetProcFirstStatementNumber(pindex) << ":" << pkb->GetProcLastStatementNumber(pindex) << endl;
	//cout << "procedure " << PKB::ast.GetValue(node) << " ";
	codeList += ("procedure " + PKB::ast.GetValue(node) + " ");
	displayStatementList(PKB::ast.GetChild(node,0));
}
void Parser::displayStatementList(TNode* node)
{
//	cout << "{" << endl;
	STRING openBracket = "{\n";
	codeList += (openBracket);
	//cout << codeList;
	TNode* currentStmt = PKB::ast.GetChild(node,0);
	while (currentStmt!=NULL)
	{
		displayStatement(currentStmt);
		currentStmt=PKB::ast.GetRightSibling(currentStmt);
	}
	//cout << "}" << endl;
	STRING closeBracket = "}\n";
	codeList += (closeBracket);
}
void Parser::displayStatement(TNode* node)
{
	if (PKB::ast.GetNodeType(node)==NODETYPE::ASSIGN)
	{
		displayAssignment(node);
	}
	else if (PKB::ast.GetNodeType(node)==NODETYPE::WHILE)
	{
		displayWhile(node);
	}
}
void Parser::displayWhile(TNode* node)
{
	//cout << pkb->GetStatementNumber(node) << " while " << PKB::ast.GetValue(PKB::ast.GetChild(node,0)) << " ";
	codeList += ("while " + PKB::ast.GetValue(PKB::ast.GetChild(node,0)) + " ");
	displayStatementList(PKB::ast.GetChild(node,1));
}
void Parser::displayAssignment(TNode* node)
{
	//cout << pkb->GetStatementNumber(node) << " " << PKB::ast.GetValue(PKB::ast.GetChild(node,0)) << " = ";
	codeList += (PKB::ast.GetValue(PKB::ast.GetChild(node,0)) + " = ");
	displayExpression(PKB::ast.GetChild(node,1));
	//cout << ";" << endl;
	STRING semiColon = ";\n";
	codeList += (semiColon);
}
void Parser::displayExpression(TNode* node)
{
	if (PKB::ast.GetNodeType(node)==NODETYPE::PLUS)
	{
		TNode* leftChild = PKB::ast.GetChild(node,0);
		TNode* rightChild = PKB::ast.GetChild(node,1);
		displayExpression(leftChild);
	//	cout << " + ";
		codeList += " + ";
		displayExpression(rightChild);
	}
	else if ((PKB::ast.GetNodeType(node)==NODETYPE::CONSTANT)||(PKB::ast.GetNodeType(node)==NODETYPE::VARIABLE))
	{
	//	cout << PKB::ast.GetValue(node);
		codeList += PKB::ast.GetValue(node);
	}
}

STRING Parser::getErrorMsg()
{
	return errorMsg;
}

STRING Parser::getCodeList()
{
	return codeList;
}